package cn.myfirefly.utils;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class ChineseUtil {
	/**
	 * 将汉字转换为全拼
	 * @param src
	 * @return
	 */
	public static String getPingYin(String src) {  
        char[] t1 = null;  
        t1 = src.toCharArray();  
        String[] t2 = new String[t1.length];  
        HanyuPinyinOutputFormat t3 = new HanyuPinyinOutputFormat();  
          
        t3.setCaseType(HanyuPinyinCaseType.LOWERCASE);  
        t3.setToneType(HanyuPinyinToneType.WITHOUT_TONE);  
        t3.setVCharType(HanyuPinyinVCharType.WITH_V);  
        String t4 = "";  
        int t0 = t1.length;  
        try {  
            for (int i = 0; i < t0; i++) {  
                // 判断是否为汉字字符  
                if (java.lang.Character.toString(t1[i]).matches(  
                        "[\\u4E00-\\u9FA5]+")) {  
                    t2 = PinyinHelper.toHanyuPinyinStringArray(t1[i], t3);  
                    t4 += t2[0];  
                } else  
                    t4 += java.lang.Character.toString(t1[i]);  
            }  
            // System.out.println(t4);  
            return t4;  
        } catch (BadHanyuPinyinOutputFormatCombination e1) {  
            e1.printStackTrace();  
        }  
        return t4;  
    }  
  
    /**
     * 返回中文的首字母  
     * @param str
     * @return
     */
    public static String getPinYinHeadChar(String str) {  
  
        String convert = "";  
        for (int j = 0; j < str.length(); j++) {  
            char word = str.charAt(j);  
            String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(word);  
            if (pinyinArray != null) {  
                convert += pinyinArray[0].charAt(0);  
            } else {  
                convert += word;  
            }  
        }  
        return convert;  
    }  
  
    /**
     * 将字符串转移为ASCII码  
     * @param cnStr
     * @return
     */
    public static String getCnASCII(String cnStr) {  
        StringBuffer strBuf = new StringBuffer();  
        byte[] bGBK = cnStr.getBytes();  
        for (int i = 0; i < bGBK.length; i++) {  
            strBuf.append(Integer.toHexString(bGBK[i] & 0xff));  
        }  
        return strBuf.toString();  
    }
    
    /**
     * 数字转换为汉子
     * @param string
     * @return
     */
	public static String ToCH(int intInput) {
    		String si = String.valueOf(intInput);
    		String sd = "";
    		if (si.length() == 1) // 個
    		{
    			sd += GetCH(intInput);
    			return sd;
    		} else if (si.length() == 2)// 十
    		{
    			if (si.substring(0, 1).equals("1"))
    				sd += "十";
    			else
    				sd += (GetCH(intInput / 10) + "十");
    			sd += ToCH(intInput % 10);
    		} else if (si.length() == 3)// 百
    		{
    			sd += (GetCH(intInput / 100) + "百");
    			if (String.valueOf(intInput % 100).length() < 2)
    				sd += "零";
    			sd += ToCH(intInput % 100);
    		} else if (si.length() == 4)// 千
    		{
    			sd += (GetCH(intInput / 1000) + "千");
    			if (String.valueOf(intInput % 1000).length() < 3)
    				sd += "零";
    			sd += ToCH(intInput % 1000);
    		} else if (si.length() == 5)// 萬
    		{
    			sd += (GetCH(intInput / 10000) + "萬");
    			if (String.valueOf(intInput % 10000).length() < 4)
    				sd += "零";
    			sd += ToCH(intInput % 10000);
    		}

    		return sd;
    	}

    	private static String GetCH(int input) {
    		String sd = "";
    		switch (input) {
    		case 1:
    			sd = "一";
    			break;
    		case 2:
    			sd = "二";
    			break;
    		case 3:
    			sd = "三";
    			break;
    		case 4:
    			sd = "四";
    			break;
    		case 5:
    			sd = "五";
    			break;
    		case 6:
    			sd = "六";
    			break;
    		case 7:
    			sd = "七";
    			break;
    		case 8:
    			sd = "八";
    			break;
    		case 9:
    			sd = "九";
    			break;
    		default:
    			break;
    		}
    		return sd;
    }
    
    public static void main(String[] args) {
    	System.out.println(getPingYin("李刚"));
    	System.out.println(ToCH(18));
    }
}
